import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { GrpcService } from 'src/common/grpc/grpc.service';
import { convertProjectionToArray } from 'src/utils';
import { UserDocument, UserEntity } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectModel(UserEntity.name)
    private userModel: Model<UserDocument>,
  ) {}

  async createUser({
    userName,
    password,
  }: {
    userName: string;
    password: string;
  }): Promise<UserEntity> {
    const newUser = new this.userModel({
      userName,
      password,
    });
    return newUser.save();
  }

  async findUser(_id: string): Promise<UserEntity> {
    return this.userModel.findById(_id);
  }

  async findUserByUserName(userName: string): Promise<UserEntity> {
    return this.userModel.findOne({ userName });
  }
}
